<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use common\components\helpers\Fun;
use common\models\datas\Student;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%comments}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property integer $user_id
 * @property integer $valuator_type
 * @property integer $valuator_id
 * @property integer $accepter_type
 * @property integer $accepter_id
 * @property integer $lesson_student_id
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class Comments extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%comments}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'valuator_type', 'valuator_id', 'accepter_type', 'accepter_id', 'lesson_student_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'description'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'title' => Yii::t('zhtfrontend', '标题'),
            'description' => Yii::t('zhtfrontend', '评论描述 '),
            'user_id' => Yii::t('zhtfrontend', '管理员id'),
            'valuator_type' => Yii::t('zhtfrontend', '评价者类型（1学生2老师3教辅总监）'),
            'valuator_id' => Yii::t('zhtfrontend', '评价者id'),
            'accepter_type' => Yii::t('zhtfrontend', '被评价者类型（1学生2老师3教辅总监）'),
            'accepter_id' => Yii::t('zhtfrontend', '被评价者id'),
            'lesson_student_id' => Yii::t('zhtfrontend', '成长日志id'),
            'status' => Yii::t('zhtfrontend', '状态（0关闭1正常）'),
            'created_at' => Yii::t('zhtfrontend', '添加时间'),
            'updated_at' => Yii::t('zhtfrontend', '修改时间'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['status'] = function () {
            switch ($this->status) {
                case 1:
                    return \Yii::t('zhtfrontend', '正常');
                case 0:
                    return \Yii::t('zhtfrontend', '关闭');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };

        $fields['valuator_type'] = function () {
            switch ($this->valuator_type) {
                case 1:
                    return \Yii::t('zhtfrontend', '学生');
                case 2:
                    return \Yii::t('zhtfrontend', '老师');
                case 3:
                    return \Yii::t('zhtfrontend', '教服总监');
            }
        };
        $fields['accepter_type'] = function () {
            switch ($this->accepter_type) {
                case 1:
                    return \Yii::t('zhtfrontend', '学生');
                case 2:
                    return \Yii::t('zhtfrontend', '老师');
                case 3:
                    return \Yii::t('zhtfrontend', '教服总监');
            }
        };
        $fields['valuator_name'] = function () {
            switch ($this->valuator_type) {
                case 1:
                    $student = Student::find()->where(['site_id'=>Fun::getSiteId(),'id'=>$this->valuator_id])->one();
                    return !empty($student->name)?$student->name:'--';
                case 2:
                    $teacher = Teacher::findOne($this->valuator_id);
                    return !empty($teacher->name)?$teacher->name:'--';
                case 3:
                    $admin = Admin::findOne($this->valuator_id);
                    return !empty($admin->name)?$admin->name:'--';
            }
        };
        $fields['accepter_name'] = function () {
            switch ($this->accepter_type) {
                case 1:
                    $student = Student::find()->where(['site_id'=>Fun::getSiteId(),'id'=>$this->accepter_id])->one();
                    return !empty($student->name)?$student->name:'--';
                case 2:
                    $teacher = Teacher::findOne($this->accepter_id);
                    return !empty($teacher->name)?$teacher->name:'--';
                case 3:
                    $admin = Admin::findOne($this->accepter_id);
                    return !empty($admin->name)?$admin->name:'--';
            }
        };

        return $fields;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];

    }

    public function getCommentRelations()
    {
        return $this->hasMany(CommentRelation::className(), ['comment_id' => 'id']);
    }

    public function getTeacher()
    {
        if($this->accepter_type == 3){
            return $this->hasOne(Teacher::className(), ['id' => 'accepter_id']);
        }
    }

    public function getInfos()
    {
        return $this->hasMany(CommentInfo::className(), ['id' => 'comment_info_id'])
            ->via('commentRelations');
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],

        ]);


    }
}
